<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="销售订单名称：">
        <el-input v-model="dataForm.name" placeholder="请输入销售订单名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="success" plain @click="_exportCSV()">导出</el-button>
        <el-button type="info" @click="clientReportVisible = true">配置导出字段</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        v-model="dataForm.name"
        label="销售订单名">
      </el-table-column>
      <el-table-column
        prop="clientName"
        header-align="center"
        align="center"
        v-model="dataForm.clientName"
        label="客户">
      </el-table-column>
      <el-table-column
        prop="staffName"
        header-align="center"
        align="center"
        v-model="dataForm.staffName"
        label="业务员">
      </el-table-column>
      <el-table-column
        prop="saleDate"
        header-align="center"
        align="center"
        label="销售日期">
      </el-table-column>
      <el-table-column
        prop="deliveryDate"
        header-align="center"
        align="center"
        label="交货日期">
      </el-table-column>
      <el-table-column
        prop="productAddress"
        header-align="center"
        align="center"
        v-model="dataForm.productAddress"
        label="安装地址">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small"
                     @click="addOrUpdateHandle(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="提示"
      :visible.sync="clientReportVisible"
      width="30%"
    >
      <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
      <div style="margin: 15px 0;"></div>
      <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
        <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
    <el-button @click="clientReportVisible = false">取 消</el-button>
    <el-button type="primary" @click=handleTitle>确 定</el-button>
  </span>
    </el-dialog>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './order-add-or-update'
  const cityOptions = ['销售订单名称', '客户名称', '职员名', '销售日期', '交货日期', '生产地址', '备注',
    '商品名称', '生产名称', '宽', '高', '数量', '方向', '面积', '材料', '颜色', '特殊工艺名称', '特殊工艺单价']
  const alertTypeIntGetStringMap = {
    '销售订单名称': 'saleOrderName',
    '客户名称': 'clientName',
    '职员名': 'staffName',
    '销售日期': 'saleDate',
    '交货日期': 'deliveryDate',
    '生产地址': 'productAddress',
    '备注': 'saleOrderRemark',
    '商品名称': 'commodityName',
    '生产名称': 'productName',
    '宽': 'width',
    '高': 'height',
    '数量': 'count',
    '方向': 'direction',
    '面积': 'squares',
    '材料': 'materialName',
    '颜色': 'colorName',
    '特殊工艺名称': 'crafts',
    '特殊工艺单价': 'craftsPrice'
  }
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          name: '',
          code: '',
          clientName: '',
          staffName: '',
          productAddress: '',
          status: '',
          warning: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        clientReportVisible: false,
        titleList: '销售订单名称,客户名称,职员名,销售日期,交货日期,生产地址,备注,商品名称,生产名称,宽,高,数量,方向,面积,材料,颜色,特殊工艺名称,特殊工艺单价',
        keyList: 'saleOrderName,clientName,staffName,saleDate,deliveryDate,productAddress,saleOrderRemark,commodityName,productName,width,height,count,direction,squares,materialName,colorName,crafts,craftsPrice',
        checkAll: true,
        checkedCities: ['销售订单名称', '客户名称', '职员名', '销售日期', '交货日期', '生产地址', '备注',
          '商品名称', '生产名称', '宽', '高', '数量', '方向', '面积', '材料', '颜色', '特殊工艺名称', '特殊工艺单价'],
        cities: cityOptions,
        isIndeterminate: true
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/pro/order/listAll'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            'code': this.dataForm.code,
            'status': this.dataForm.status,
            'warning': this.dataForm.warning
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/pro/order/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      _exportCSV () {
        this.$http({
          url: this.$http.adornUrl('/pro/order/exportCSV'),
          method: 'post',
          responseType: 'blob',
          params: this.$http.adornParams({
            'titles': this.titleList,
            'keys': this.keyList,
            'saleOrderName': this.dataForm.name
          })
        }).then((response) => {
          if (response) {
            let url = window.URL.createObjectURL(new Blob([response.data], {type: 'application/ms-txt.numberformat:@'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', '生产单.csv')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
          } else {
            this.$message.error('请设置要导出的字段')
          }
        })
      },
      handleCheckAllChange (val) {
        this.checkedCities = val ? cityOptions : []
        this.isIndeterminate = false
      },
      handleCheckedCitiesChange (value) {
        let checkedCount = value.length
        this.checkAll = checkedCount === this.cities.length
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length
      },
      handleTitle () {
        this.titleList = ''
        this.keyList = ''

        for (let i = 0, leng = this.checkedCities.length; i < leng; i++) {
          let title = this.checkedCities[i]

          let key = alertTypeIntGetStringMap[title]

          if (i !== leng - 1) {
            this.titleList += title + ','

            this.keyList += key + ','
          } else {
            this.titleList += title

            this.keyList += key
          }
        }

        this.clientReportVisible = false
      }
    }
  }
</script>
